{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "83d91910",
   "metadata": {},
   "source": [
    "# 字典的方法 \n",
    "> 1. dict.clear()\n",
    ">> 删除字典内所有元素\n",
    "> 2. dict.copy()\n",
    ">> 返回一个字典的浅复制\n",
    "> 3. dict.fromkeys()\n",
    ">> 创建一个新字典，以序列seq中元素做字典的键，val为字典所有键对应的初始值\n",
    "> 4. dict.get(key, default=None)\n",
    ">> 返回指定键的值，如果键不在字典中返回 default 设置的默认值\n",
    "> 5. key in dict\n",
    ">> 如果键在字典dict里返回true，否则返回false\n",
    "> 6. dict.items()\n",
    ">> 以列表返回一个视图对象\n",
    "> 7. dict.keys()\n",
    ">> 返回一个视图对象\n",
    "> 8. dict.setdefault(key, default=None)\n",
    ">> 和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default\n",
    "> 9. dict.update(dict2)\n",
    ">> 把字典dict2的键/值对更新到dict里\n",
    "> 10. dict.values()\n",
    ">> 返回一个视图对象\n",
    "> 11. pop(key[,default])\n",
    ">> 删除字典 key（键）所对应的值，返回被删除的值。\n",
    "> 12. popitem()\n",
    ">> 返回并删除字典中的最后一对键和值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d5aa63",
   "metadata": {},
   "source": [
    "## 1. clear（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "c5c76f2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20}\n",
    "len(tinydict) # 字典长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "id": "6b34e241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tinydict.clear() # 删除\n",
    "len(tinydict) # 删除后的长度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14a7ff6d",
   "metadata": {},
   "source": [
    "## 2. dict.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "id": "602bf4cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新复制的字典为 :  {'Name': 'Hmy', 'Age': 20, 'Class': 'First'}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'Name': 'Hmy', 'Age': 20, 'Class': 'First'} \n",
    "dict2 = dict1.copy() # 复制dict1\n",
    "print (\"新复制的字典为 : \",dict2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b56c338",
   "metadata": {},
   "source": [
    "##  3. dict.fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "id": "0421be26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新的字典为 : {'name': None, 'age': None, 'sex': None}\n",
      "新的字典为 : {'name': 10, 'age': 10, 'sex': 10}\n"
     ]
    }
   ],
   "source": [
    "seq = ('name', 'age', 'sex')\n",
    " \n",
    "tinydict = dict.fromkeys(seq)\n",
    "print (\"新的字典为 : %s\" %  str(tinydict))\n",
    " \n",
    "tinydict = dict.fromkeys(seq, 10)\n",
    "print (\"新的字典为 : %s\" %  str(tinydict))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eef3c487",
   "metadata": {},
   "source": [
    "##  4. dict.get(key, default=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aa71dde",
   "metadata": {},
   "source": [
    "* 1. key -- 字典中要查找的键。\n",
    "* 2. value -- 可选，如果指定键的值不存在时，返回该默认值。\n",
    "* 返回值: 返回指定键的值，如果键不在字典中返回默认值 None 或者设置的默认值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "8273d81a",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "ff218ec6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age :  20\n"
     ]
    }
   ],
   "source": [
    "print (\"Age : \", tinydict.get('Age'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "21ed61ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sex :  None\n"
     ]
    }
   ],
   "source": [
    "# 没有设置 Sex，也没有设置默认的值，输出 None\n",
    "print (\"Sex : \", tinydict.get('Sex'))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "id": "86f59059",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Salary:  0.0\n"
     ]
    }
   ],
   "source": [
    "# 没有设置 Salary，输出默认的值  0.0\n",
    "print ('Salary: ', tinydict.get('Salary', 0.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8eec3c0c",
   "metadata": {},
   "source": [
    "## 5. key in dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c1c5fc7",
   "metadata": {},
   "source": [
    "* 如果键在字典 dict 里返回 true，否则返回 false。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "id": "cea2f14b",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "fdf87862",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键 Age 存在\n"
     ]
    }
   ],
   "source": [
    "# 检测键 Age 是否存在\n",
    "if  'Age' in thisdict:\n",
    "    print(\"键 Age 存在\")\n",
    "else :\n",
    "    print(\"键 Age 不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "e10fee4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键 Sex 不存在\n"
     ]
    }
   ],
   "source": [
    "# 检测键 Sex 是否存在\n",
    "if  'Sex' in thisdict:\n",
    "    print(\"键 Sex 存在\")\n",
    "else :\n",
    "    print(\"键 Sex 不存在\")\n",
    "\n",
    "# not in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "6dceb3d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键 Age 存在\n"
     ]
    }
   ],
   "source": [
    "# 检测键 Age 是否存在\n",
    "if  'Age' not in thisdict:\n",
    "    print(\"键 Age 不存在\")\n",
    "else :\n",
    "    print(\"键 Age 存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b67771a4",
   "metadata": {},
   "source": [
    "## 6. dict.items() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "378d7a42",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "id": "7b63ac23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('Name', 'Hmy'), ('Age', 20)])\n"
     ]
    }
   ],
   "source": [
    "print(tinydict.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ec01340",
   "metadata": {},
   "source": [
    "## 7. dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "5d7fe003",
   "metadata": {},
   "outputs": [],
   "source": [
    "dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "88d54bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = dishes.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "b8da74f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "values = dishes.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "id": "26c50194",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "504\n"
     ]
    }
   ],
   "source": [
    "# 迭代\n",
    "n = 0\n",
    "for val in values:\n",
    "    n += val\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "1449b4db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['eggs', 'sausage', 'bacon', 'spam']"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# keys 和 values 以相同顺序（插入顺序）进行迭代\n",
    "list(keys)     # 使用 list() 转换为列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "id": "4bc57454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 1, 1, 500]"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "45ce24b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bacon', 'spam']"
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 视图对象是动态的，受字典变化的影响，以下删除了字典的 key，视图对象转为列表后也跟着变化\n",
    "del dishes['eggs']\n",
    "del dishes['sausage']\n",
    "list(keys) # 打印list剩下的key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71a62f0a",
   "metadata": {},
   "source": [
    " ## 8. dict.setdefault(key, default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "e67876b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20}  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "ea7b5fc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 键的值为 : 20\n",
      "Sex 键的值为 : None\n",
      "新字典为： {'Name': 'Hmy', 'Age': 20, 'Sex': None}\n"
     ]
    }
   ],
   "source": [
    "print (\"Age 键的值为 : %s\" %  tinydict.setdefault('Age', None))\n",
    "print (\"Sex 键的值为 : %s\" %  tinydict.setdefault('Sex', None))\n",
    "print (\"新字典为：\", tinydict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "401850f7",
   "metadata": {},
   "source": [
    "## 9. dict.update(dict2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "2698da79",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict = {'Name': 'Hmy', 'Age': 20} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "id": "de96fdcb",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict2 = {'Sex': 'female' } "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "47166f0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "tinydict.update(tinydict2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "735f6406",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新字典 tinydict :  {'Name': 'Hmy', 'Age': 20, 'Sex': 'female'}\n"
     ]
    }
   ],
   "source": [
    "print (\"更新字典 tinydict : \", tinydict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d26ddcd",
   "metadata": {},
   "source": [
    "## 10. dict.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "b59e264a",
   "metadata": {},
   "outputs": [],
   "source": [
    "dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}\n",
    "keys = dishes.keys()\n",
    "values = dishes.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "a6f182d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "504\n"
     ]
    }
   ],
   "source": [
    "# 迭代\n",
    "n = 0\n",
    "for val in values:\n",
    "    n += val\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "3d9be945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['eggs', 'sausage', 'bacon', 'spam']"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# keys 和 values 以相同顺序（插入顺序）进行迭代\n",
    "list(keys)     # 使用 list() 转换为列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "id": "752c2725",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 1, 1, 500]"
      ]
     },
     "execution_count": 256,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "61afcdf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 500]"
      ]
     },
     "execution_count": 257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 视图对象是动态的，受字典变化的影响，以下删除了字典的 key，视图对象转为列表后也跟着变化\n",
    "del dishes['eggs']\n",
    "del dishes['sausage']\n",
    "list(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "id": "2b5ac456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#相同两个 dict.values() 比较返回都是 False\n",
    "d = {'a': 1}\n",
    "d.values() == d.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa098aea",
   "metadata": {},
   "source": [
    "## 11. pop(key[,default])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "id": "aef4918d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除的元素为: gitee\n",
      "字典为: {'alexa': 10000, 'url': 'https://gitee.com'}\n"
     ]
    }
   ],
   "source": [
    "site= {'name': 'gitee', 'alexa': 10000, 'url': 'https://gitee.com'}\n",
    "element = site.pop('name')\n",
    "print('删除的元素为:', element)\n",
    "print('字典为:', site)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5925bfa4",
   "metadata": {},
   "source": [
    "## 12. popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "99ae332e",
   "metadata": {},
   "outputs": [],
   "source": [
    "site= {'name': 'gitee', 'alexa': 10000, 'url': 'https://gitee.com'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "e29a5f72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('url', 'https://gitee.com')\n",
      "{'name': 'gitee', 'alexa': 10000}\n"
     ]
    }
   ],
   "source": [
    "# ('url': 'https://gitee.com') 最后插入会被删除\n",
    "result = site.popitem()\n",
    "print(result)\n",
    "print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "98cc9405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'gitee', 'alexa': 10000, 'nickname': '码云'}\n"
     ]
    }
   ],
   "source": [
    "# 插入新元素\n",
    "site['nickname'] = '码云'\n",
    "print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "0b1f6823",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('nickname', '码云')\n",
      "{'name': 'gitee', 'alexa': 10000}\n"
     ]
    }
   ],
   "source": [
    "# 现在 ('nickname', '码云') 是最后插入的元素\n",
    "result = site.popitem()\n",
    "print(result)\n",
    "print(site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c90dd20",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
